<template>
  <div class="login_container" ref="vantaRef">
    <div class="login_box">
      <!-- LOGO区 -->
      <div class="avatar_box">
       <img src="https://ts1.cn.mm.bing.net/th/id/R-C.24a7f9a3a939b12fdd6d54d280255c6a?rik=Ib50cKnIV6EWeA&riu=http%3a%2f%2fwww.58.com%2fzp_images%2fallimg%2f120913%2f5_120913173734_1.png&ehk=qlFxNq3%2b8R0ITYoTEWyYswlswZJ7dokSxA7QriJyznE%3d&risl=&pid=ImgRaw&r=0" alt="" >
      </div>
      <!-- 登录表单区域 -->
      <el-form :model="loginForm" :rules="loginRules" ref="loginFormRef" class="login_form">
        <!-- 用户名 -->
        <el-form-item prop="stu_phone">
          <el-input
            v-model="loginForm.stu_phone"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="stu_password">
          <el-input
            type="password"
            v-model="loginForm.stu_password"
            prefix-icon="iconfont icon-3702mima"
            @keyup.enter.native="loginFormSubmit"
          ></el-input>
        </el-form-item>
        <!-- 登录按钮、重置按钮 -->
        <el-form-item class="btns">
          <el-button  round @click="loginFormSubmit">登录</el-button>
          <el-button  round @click="loginFormReset">职工登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import * as THREE from 'three'
import birds from 'vanta/src/vanta.birds'
import { mapMutations } from 'vuex'
import { StuLogin } from '@/api/Login/index'
import axios from 'axios'
export default {
  name: 'loginForm',
  data () {
    return {
      // 表单数据绑定对象
      loginForm: {
        stu_phone: '',
        stu_password: ''
      },
      // 表单输入框验证规则
      loginRules: {
        stu_phone: [
        { required: true, message: '请输入手机号', trigger: 'blur' },
         {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: '电话号码格式不对',
            trigger: 'blur'
          },
        ],
        stu_password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // ...mapMutations(['UPDATEUSERINFO ']),
    loginFormReset () {
      // 重置表单调用
      this.$router.push('adminlogin')
    },
    loginFormSubmit () {
      this.$refs.loginFormRef.validate(async valid => {
        // 判断是否要发起请求，如果为false代表表单验证不通过，不发起请求
        if (!valid) {
          return 0
        } else {
          console.log(123)
          const { data: res } = await StuLogin(this.loginForm.stu_phone,this.loginForm.stu_password)
          console.log("1233",res)
         if (res.code === 0&&res.status!==false) {
            this.$store.dispatch('initStuInfo', this.loginForm.stu_phone)
            this.$message.success('登录成功')
            this.$router.push('before_main')
            // window.sessionStorage.setItem('token', res.data.accessToken)
            // 通过编程式导航跳转到后台主页，路由地址是home     
          } else {
            this.$message.error(res.message)
          }
        }
      })
    }
  },
  mounted () {
    this.vantaEffect = birds({
      el: this.$refs.vantaRef,
      THREE: THREE
    })
    this.vantaEffect.setOptions({
   mouseControls: true,
  touchControls: true,
  gyroControls: false,
  minHeight: 200.00,
  minWidth: 200.00,
  scale: 1.00,
  scaleMobile: 1.00,
  backgroundColor: 0x35e8e8,
  birdSize: 4.00
    })
  },
  beforeDestroy () {
    if (this.vantaEffect) {
      this.vantaEffect.destroy()
    }
    }
}
</script>
<style scoped>
.login_container {
  background-color: #ecf0f396;
  width: 100%;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  box-shadow: 10px 10px 10px #d1d9e67f, -10px -10px 10px #d1d9e67f;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #ecf0f396;
  box-shadow: 10px 10px 10px #d1d9e67f, -10px -10px 10px #d1d9e67f;
}

.avatar_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
}

.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
